﻿using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WaferSelectV3.Common;
using WaferSelectV3.EventsManager;
using WaferSelectV3.Services;

namespace WaferSelectV3.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;
        public MainWindow(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            InitializeComponent();
            WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
            Loaded += MainWindow_Loaded;
            SizeChanged += MainWindowSizeChanged;
            // 最大化的时候不遮挡下面的任务栏的逻辑
            //MaxHeight = SystemParameters.PrimaryScreenHeight;

            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
        }

        private void MainWindowSizeChanged(object sender, SizeChangedEventArgs e)
        {
            WindowSize newSzie = new WindowSize() { Width = ActualWidth, Height = ActualHeight };
            _eventAggregator.GetEvent<WindowSizeChangedEvent>().Publish(newSzie);
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // 启动监听程序是否已经启动的任务
            SingleInstanceManager.AppStartEventListen(() =>
            {
                // 这里要操控UI,所以这里最好是在UI线程上去操作
                Dispatcher.Invoke(new Action(() =>
                {
                    if (WindowState == WindowState.Minimized)
                    {
                        WindowState = WindowState.Normal;
                    }
                    Activate();
                }));
            });

            // 1. 设置导航首页
            _regionManager.Regions[RegionNameManager.MainRegionContent].RequestNavigate(nameof(IndexView));
            // 2. Path Dir 目录创建,程序中用到的一些目录创建
            PathDirProvider.CreateDir();
            WindowSize newSzie = new WindowSize() { Width = ActualWidth, Height = ActualHeight };
            _eventAggregator.GetEvent<WindowSizeChangedEvent>().Publish(newSzie);
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void BtnMin_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void BtnMax_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            // 发布程序关闭事件,程序关闭的时候发布这个事件就行,在对应的ViewModel里面订阅这个事件
            _eventAggregator.GetEvent<AppCloseEvent>().Publish();
        }

    }
}
